package com.test;

import com.ProvierAndCustoer.Bread;
import com.ProvierAndCustoer.Customer;

import java.util.concurrent.TimeUnit;

/**
 * Created by 41579 on 2017/4/20.
 */
public class CustomerRunnable implements Runnable{
    Customer<Bread> customer;

    public CustomerRunnable(Customer<Bread> customer) {
        this.customer = customer;
    }

    @Override
    public void run() {
        while (true){
            synchronized (customer.getInventory())
            {
                if(customer.getInventory().get().size()>0){
                    customer.custom();
                    System.out.println(Thread.currentThread().getName() + " reduce "+customer.getInventory().get().size());
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    try {
                        customer.getInventory().notifyAll();
                        customer.getInventory().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
